/*
 * 原版接口
 */
import * as server from "@minecraft/server";
/*
 * 蛋白石组件
 */
import * as realm_property from "../system/realm_property";
import * as derived_tools from "../system/derived_tools";
import * as native_tools from "../system/native_tools";
import * as achieve_plan from "../system/achieve_plan";
import * as math_tools from "../system/math_tools";

/**
 * * 能量节点 < 能量补充 >
 */
export function Supply(block: server.Block, value: number) {
    /**
     * * 补充 星尘能 消耗
     */
    const energy = realm_property.AlterEnergy(block, value, true);
    // 显示 魔晶网络 - 星尘值
    derived_tools.AlterMessageNotify('<§l§b 能量节点 §r>§s 星尘力产出§r', block, { text: '<§l§d 星尘力 §r> : §l§u' + energy[1] + '§r' });
};

/**
 * * 储罐能量
 */
export function Tank(object: server.Block) {
    /**
     * * 获取 目标方块
     */
    const target = object.above();
    // 判断 方块 是否 存在
    if (!target || target.isAir || !target.isValid()) return;
    // 切换运行阶段
    native_tools.TrySetPermutation(target, 'STATE:stage', 1);
};

/**
 * * 查询与激活 动能分配
 * 
 * @param {server.Block} block - 发出侦测的能量源方块
 * 
 * @param {server.Vector3} offset - 方向向量
 */
function EnergyAllocation(block: server.Block, offset: server.Vector3) {
    // 遍历 动能分配模块
    for (let index = 1; index < 6; index++) {
        /**
         * * 获取 目标方块
         */
        const target = block.offset(math_tools.Vector.scale(offset, index));
        // 判断 方块 是否 是 动能分配模块
        if (!target || !target.hasTag('data_tag:energy_module.allocation')) continue;
        /**
         * * 获取 方块状态
         */
        const states = target.permutation;
        // 如果该方块已经被充能就跳过该方块
        if ((states.getState('STATE:output') as number) == 1) continue;
        // 改变方块状态
        target.setPermutation(states.withState('STATE:output', 1));
    }
};
/**
 * * 检测周围是否有能量分配
 * 
 * @param {server.Block} block - 发出侦测的能量源方块
 * 
 * @returns {boolean} - 是否有 动能分配模块
 */
function CheckAllocation(block: server.Block): boolean {
    /**
     * * 获取 附近的方块
     */
    const entry = [block.east(), block.west(), block.north(), block.south()];
    // 遍历 方块 并测试 方块是否是动能分配模块
    for (let index = 0; index < entry.length; index++) {
        if (entry[index]?.hasTag('data_tag:energy_module.allocation')) return true;
    };
    // 返回结果
    return false;
};

/**
 * * 风力能量
 */
export function Wind(block: server.Block) {
    /**
     * * 获取 自身 的 方块状态
     */
    const permutation = block.permutation;
    /**
     * * 获取 当前运行阶段
     */
    const current = permutation.getState('STATE:stage') as number;
    // 判断 方块 是否 有 动能分配模块
    if (!CheckAllocation(block)) return;
    // 判断方块是否在 高度阈值内
    if (block.y > 200 || block.y < 64) return;
    /**
     * * 计算 阈值
     */
    const threshold = Math.floor(9 - ((block.y - 64) / 15));
    // 判断阈值 是否 等于 当前运行阶段
    if (current < threshold) return;
    // 播放 风力叶片粒子效果
    if (achieve_plan.ControlEventTrigger.BlockToken(block.typeId, block, 60)) {
        /**
         * * 定义 粒子参数
         */
        const molang = new server.MolangVariableMap();
        molang.setFloat('variable.direction', 2);
        molang.setFloat('variable.size', 16);
        // 播放 粒子效果
        native_tools.TrySpawnParticle(block.dimension, 'scripts:path_round', math_tools.Vector.add(block.location, math_tools.Vector.CONSTANT_HALF), molang);
        native_tools.TrySpawnParticle(block.dimension, 'scripts:path_label', math_tools.Vector.add(block.location, math_tools.Vector.CONSTANT_HALF), molang);
    };
    // 基于 方块朝向 遍历 动能分配模块
    switch (permutation.getState('minecraft:cardinal_direction')) {
        case 'south': EnergyAllocation(block, math_tools.Vector.CONSTANT_SOUTH); break;

        case 'north': EnergyAllocation(block, math_tools.Vector.CONSTANT_NORTH); break;

        case 'east': EnergyAllocation(block, math_tools.Vector.CONSTANT_EAST); break;

        case 'west': EnergyAllocation(block, math_tools.Vector.CONSTANT_WEST); break;

        default: break;
    };
    // 切换运行阶段
    native_tools.TrySetPermutation(block, 'STATE:stage', 9);
};

/**
 * * 水素能量
 * 
 * @param block - 目标方块
 */
export function Water(block: server.Block) {
    /**
     * * 获取 自身 的 方块状态
     */
    const permutation = block.permutation;
    // 判断 方块 是否 有 动能分配模块
    if (!CheckAllocation(block)) return;
    // 切换运行阶段
    native_tools.TrySetPermutation(block, 'STATE:stage', 0);
    // 基于 方块朝向 遍历 动能分配模块
    switch (permutation.getState('minecraft:cardinal_direction')) {
        case 'south': EnergyAllocation(block, math_tools.Vector.CONSTANT_SOUTH); break;

        case 'north': EnergyAllocation(block, math_tools.Vector.CONSTANT_NORTH); break;

        case 'east': EnergyAllocation(block, math_tools.Vector.CONSTANT_EAST); break;

        case 'west': EnergyAllocation(block, math_tools.Vector.CONSTANT_WEST); break;

        default: break;
    };
    // 播放 水花 粒子效果
    achieve_plan.SprayParticleTrigger.BriefCreate('水素能源模块的运行特效', block.center(), block.dimension);
};

/**
 * * 熔岩能量
 * 
 * @param block - 目标方块
 */
export function Magma(block: server.Block) {
    // 判断 方块 是否 有 动能分配模块
    if (!CheckAllocation(block)) return;
    // 遍历 动能分配模块
    EnergyAllocation(block, math_tools.Vector.CONSTANT_SOUTH);
    EnergyAllocation(block, math_tools.Vector.CONSTANT_NORTH);
    EnergyAllocation(block, math_tools.Vector.CONSTANT_EAST);
    EnergyAllocation(block, math_tools.Vector.CONSTANT_WEST);
};